package cn.community.care.module.system.controller.admin.inventoryinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.CommonResult;
import cn.community.care.framework.common.util.object.BeanUtils;
import static cn.community.care.framework.common.pojo.CommonResult.success;

import cn.community.care.framework.excel.core.util.ExcelUtils;

import cn.community.care.framework.operatelog.core.annotations.OperateLog;
import static cn.community.care.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.community.care.module.system.controller.admin.inventoryinfo.vo.*;
import cn.community.care.module.system.dal.dataobject.inventoryinfo.InventoryInfoDO;
import cn.community.care.module.system.service.inventoryinfo.InventoryInfoService;

@Tag(name = "管理后台 - 库存信息")
@RestController
@RequestMapping("/system/inventory-info")
@Validated
public class InventoryInfoController {

    @Resource
    private InventoryInfoService inventoryInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建库存信息")
    public CommonResult<Long> createInventoryInfo(@Valid @RequestBody InventoryInfoSaveReqVO createReqVO) {
        return success(inventoryInfoService.createInventoryInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存信息")
    public CommonResult<Boolean> updateInventoryInfo(@Valid @RequestBody InventoryInfoSaveReqVO updateReqVO) {
        inventoryInfoService.updateInventoryInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteInventoryInfo(@RequestParam("id") Long id) {
        inventoryInfoService.deleteInventoryInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<InventoryInfoRespVO> getInventoryInfo(@RequestParam("id") Long id) {
        InventoryInfoDO inventoryInfo = inventoryInfoService.getInventoryInfo(id);
        return success(BeanUtils.toBean(inventoryInfo, InventoryInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库存信息分页")
    public CommonResult<PageResult<InventoryInfoRespVO>> getInventoryInfoPage(@Valid InventoryInfoPageReqVO pageReqVO) {
        PageResult<InventoryInfoDO> pageResult = inventoryInfoService.getInventoryInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InventoryInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库存信息 Excel")
    @OperateLog(type = EXPORT)
    public void exportInventoryInfoExcel(@Valid InventoryInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InventoryInfoDO> list = inventoryInfoService.getInventoryInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库存信息.xls", "数据", InventoryInfoRespVO.class,
                        BeanUtils.toBean(list, InventoryInfoRespVO.class));
    }

}